%% ------------------------------------------------------------------------
%% Copyright (C) 2022 LogCreative <logcreative@outlook.com>
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%% 
%%     http://www.apache.org/licenses/LICENSE-2.0
%% 
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% ------------------------------------------------------------------------
% 设置文件元数据
\ProvidesFile{sjtubeamerthemesjtug.ltx}[2022/05/07 SJTUG subtheme for SJTUBeamer]
% 设置组织名称
\institute[SJTU Linux User Group]{上海交通大学 Linux 用户组}
% 设置徽标
\logo{\resizebox{1cm}{!}{\includegraphics{\getcontribpath{sjtug}{vi/sjtug.pdf}}}}
% 设置头图
\titlegraphic{\includegraphics{vi/sjtu-vi-sjtuphoto.jpg}}
% 设定主色
\definecolor{cprimary}{RGB}{225,111,11}
% 定义自定可变色 logo
\definelogo{sjtugtext}{\getcontribpath{sjtug}{vi/sjtugtext.pdf}}{0}{0.8}
% 设置背景为 SJTUG 文字
\setbeamertemplate{background}{
  \bgcenterbox{
    \sjtugtext[cprimary, opacity=0.1]
  }
}
% 判断传递的选项
\if\EqualOption{sjtug}{debug}{true}
  \PackageWarning{sjtug}{
    You are debugging \CurrentFilePath/\CurrentFile. \MessageBreak
    Use lualatex compilation to see the visual reference line
  }
  \usepackage{lua-visual-debug}
\fi
% 制作自定标题页
\defbeamertemplate*{title page}{sjtug}[1][]{
  \vbox{}
  \usebeamercolor{palette primary}
  \begin{tikzpicture}[overlay]
    \fill [palette primary.bg] (-0.2*\the\paperwidth,-1*\the\paperheight)
    rectangle (1*\the\paperwidth, 0.3*\the\paperheight);
  \end{tikzpicture}
  \vfill
  \color{palette primary.fg}
  \hbox to \textwidth{
    \hfil
    \vbox to 3.5cm{
        \vskip3pt
        \hbox{\resizebox{2.5cm}{!}{\insertlogo}}
        \hbox{\hfill\,\,\sjtugtext[palette primary.fg]\,\hfill}
        \vfill
      }
    \hfil
    \vbox to 3.5cm{
        \vfill
        \hbox{\LARGE\bfseries\inserttitle}
        \vskip4pt
        \hbox{\large\insertsubtitle}
        \vskip2pt
        \hbox{\scriptsize\insertauthor}
        \hbox{\scriptsize\insertdate}
        \vfill
      }
    \hskip20pt
    \hfil
  }
  \vfill
}
% 设置结束页
\setbeamertemplate{bottom page}[min]
% 设置部分页、节页和小节页
\setbeamertemplate{sectioning pages}[min]
% 当然也可以分别设置：
% \setbeamertemplate{part page}[min]
% \setbeamertemplate{section page}[min]
% \setbeamertemplate{subsection page}[min]

% 下面不属于常规操作，会导致徽标不可更改，
% 不使用时请注释掉
%
% 更改徽标为含有当前章节名称
\xdef\oldlogo{\expandbeamertemplate{logo}}
\logo{\insertpart\oldlogo}
% 重定义标题页命令以局部使用老徽标定义
\let\oldmaketitle=\maketitle
\def\maketitle{
  {
    \logo{\oldlogo}
    \oldmaketitle
  }
}
% 重定义结尾页命令以局部使用老徽标定义
\let\oldmakebottom=\makebottom
\def\makebottom{
  {
    \logo{\oldlogo}
    \oldmakebottom
  }
}
% 新的数字节标题环境
\newenvironment{shadedsection}{
  \newcommand{\shadedfont}[2][1pt]{
  % ##1 (optional): shadow distance
  % ##2: the text needed to be shaded
    \hbox{\rlap{\color{gray}\hskip##1##2}##2}
  }
  \setcounter{section}{-1}
  \newcommand{\updatelogo}{
    \logo{
      \raise.3ex\hbox{\tiny\insertsection}\shadedfont{\arabic{section}}
    }
  }
  \let\oldsection=\section
  \renewcommand{\section}[1]{\oldsection{##1}\updatelogo}
}{}
